<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
	<title>YYJ</title>
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/csslib/easyui/themes/gray/easyui.css">
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/csslib/easyui/themes/icon.css">
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/csslib/myicon/myicon.css">
	<script type="text/javascript" src="${pageContext.request.contextPath }/jslib/jquery-1.7.1.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath }/jslib/easyui/jquery.easyui.124.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath }/jslib/easyui/easyui-lang-zh_CN.js"></script>
	
	<style type="text/css">
		<!--
		.op-form table tr{
			padding-top: 6px;
			font-size: 12px;
		}
		-->
	</style>
	
</head>

<body style="text-align: center;">
	
	<div id="loginWindow" icon="icon-key" style="width: 300px; height: 204px; padding: 4px; background: #fafafa; ">
       	<div class="easyui-layout" fit="true">
           	<div region="center" border="false" style="padding: 10px; background: #fff; border: 1px solid #ccc;">
            <form class="op-form" id="loginForm" name="loginForm" method="post">
                <table>
                    <tr>
                        <td>账 户：</td>
                        <td>
                        	<input class="easyui-validatebox" type="text" id="code" name="code" required="true" validType="length[2,12]" style="width: 150px;">
                        </td>
                    </tr>
                    <tr>
                        <td>密 码：</td>
                        <td>
                        	<input class="easyui-validatebox" type="password" id="psw" name="psw" required="true" validType="length[1,12]" style="width: 150px;">
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                        	<input style="clear: both;" type="checkbox" name="checkbox" id="savePsw" />记住我
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                        	<div id="loginResult" style="padding-top: 12px; color: red;"></div>
                        </td>
                    </tr>
                </table>
            </form>
           	</div>
           	<div region="south" border="false" style="text-align: right; height: 30px; line-height: 30px;">
				<a id="btnSubmit" class="easyui-linkbutton" icon="icon-user" href="#" onclick="$('#loginForm').submit()" >登录</a>
				<a class="easyui-linkbutton" icon="icon-cancel" href="#" onclick="exitSystem();">取消</a>
           	</div>
       	</div>
	</div>
	
<script type="text/javascript">
	
jQuery(function($){

	$('#loginWindow').window({
		collapsible: false,
		closable: false, 
		draggable: false,
		closed: false,
		title: 'YYJ - 登录入口'
	});
	
	var a = getCookie("code");
	$("input[name=code]").val(a);
	var b = getCookie("psw");
	$("input[name=psw]").val(b);

	// form校验
	$('#loginForm').form({
		onSubmit:function(){
			if($(this).form('validate')){
				ajaxLogin();
			}else{
				return false;
			}
		}
	});
	// 绑定回车事件
	$('input').bind('keyup', function(event){   
		if(event.keyCode == 13){   
			$('#loginForm').submit()   
        }   
    });
    
    $("#code").focus();
});

function ajaxLogin(){
	$("#btnSubmit").linkbutton({disabled: true});
	var actionURL = "user.action";
	var params = {
		command: "login",
		code: $("#code").val(),
		psw: $("#psw").val()
	};
	
	var savePsw = $("#savePsw").attr("checked");
		
	jQuery.ajax({
		url: actionURL,
		type: "post",
		cache: false,
		data: params,
		success: function(result){
			if("true" == result){
				if(savePsw){
					setCookie("code", params.code);
					setCookie("psw", params.psw);
				}
				window.location.href = "${pageContext.request.contextPath}/application/manager/main.jsp";
			}else{
				$("#loginResult").text("您的账户或密码不正确, 请重新输入!");
				$("#btnSubmit").linkbutton({disabled: false});
				/* $.messager.alert("友情提示", "您的账户或密码不正确!", "error", function(){
					loginForm.reset();
					$("#btnSubmit").linkbutton({disabled: false});
				}); */
			}
		}
	});
}

// 保存cookies
function setCookie(name, value) {
	var days = 30; // 此 cookie 将被保存 30 天
	var exp = new Date(); // new Date( "December 31, 9998 ");
	exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
	document.cookie = name + "= " + escape(value) + ";expires= "+ exp.toGMTString();
}

// 读取cookies
function getCookie(name){
	var arrStr = document.cookie.split("; ");
	for(var i = 0;i < arrStr.length;i ++){
		var temp = arrStr[i].split("=");
		if(temp[0] == name) return unescape(temp[1]);   
	}
}

function exitSystem(){
	$.messager.confirm('系统提示', '您确定要退出本次登录吗?', function(result) {
		if(result){
			window.opener = "meizz";
			window.close();
		}
	});
}

</script>

</body>
</html>
